package account;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AccountModify extends HttpServlet
{

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException 
	{
		this.doGet(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException 
	{
		String newPass = String.valueOf(request.getParameter("newPass"));
		String oldPass = String.valueOf(request.getParameter("oldPass"));
		String usuario = String.valueOf(request.getSession().getAttribute("userName"));
		String page = String.valueOf(request.getParameter("page"));
		

		
		if(request.getSession().getAttribute("loginState").equals("logged") && !newPass.equals("null"))
		{
			if(AccountDB.setPass(usuario, oldPass, newPass))
			{
				request.getSession().setAttribute("password", newPass);
				request.getSession().setAttribute("oldPass", oldPass);
				request.getSession().setAttribute("userName", usuario);
				request.getSession().setAttribute("loginStatus", "logged");
				
				RequestDispatcher rdIndex = request.getRequestDispatcher(page);
				rdIndex.forward(request, response);
			}
			else
			{
				request.setAttribute("changed", "error");
				RequestDispatcher rdIndex = request.getRequestDispatcher(page);
				rdIndex.forward(request, response);
			}
		}
		else
		{
			request.setAttribute("changed", "error");
			RequestDispatcher rdIndex = request.getRequestDispatcher(page);
			rdIndex.forward(request, response);
		}
	}
	
}
 